package library.and.xps.com.step.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.lguipeng.library.animcheckbox.AnimCheckBox;
import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;

import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.CreateActiveParams;

public class ActiveTypeSetupFragment extends Fragment implements AnimCheckBox.OnCheckedChangeListener {
    AnimCheckBox animCheckBoxGPS;
    AnimCheckBox animCheckBoxPedo;
    AutoLinearLayout snake_achor_view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setup_active_type, null);
        animCheckBoxPedo = (AnimCheckBox) view.findViewById(R.id.cb_pedo);
        animCheckBoxGPS = (AnimCheckBox) view.findViewById(R.id.cb_gps);
        snake_achor_view = (AutoLinearLayout) view.findViewById(R.id.snake_achor_view);
        animCheckBoxPedo.setOnCheckedChangeListener(this);
        animCheckBoxGPS.setOnCheckedChangeListener(this);

        return view;
    }

    public boolean next() {
        if (!animCheckBoxGPS.isChecked() && !animCheckBoxPedo.isChecked()) {
            Snackbar.make(snake_achor_view, "请至少选择一项", Snackbar.LENGTH_LONG)
                    .setAction("确定", null).show();
            return false;
        }

        CreateActiveParams createActiveParams = new CreateActiveParams();
        if(animCheckBoxGPS.isChecked()){
            createActiveParams.setType("GPS室外运动排位赛");
        }else if(animCheckBoxPedo.isChecked()){
            createActiveParams.setType("计步排位赛");
        }

        EventBus.getDefault().post(createActiveParams);

        return true;
    }

    @Override
    public void onChange(AnimCheckBox animCheckBox, boolean b) {
        if (animCheckBox.getId() == R.id.cb_gps && b) {
            animCheckBoxPedo.setChecked(!b);
        } else if (animCheckBox.getId() == R.id.cb_pedo && b) {
            animCheckBoxGPS.setChecked(!b);
        }
    }
}
